package cn.gzsxt.music.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.gzsxt.music.mapper.MapperSqlProvider;
import cn.gzsxt.music.query.SongQueryObject;
import cn.gzsxt.music.service.SongService;
import cn.gzsxt.music.util.GetMsgUtil;
import cn.gzsxt.music.vo.MsgObj;

@Controller
@Scope("request")
@RequestMapping(value="/song")
public class SongController {
	
	@Autowired
	private SongService service;
	
	@Autowired
	private GetMsgUtil msg;
	
	@Autowired
	private HttpServletRequest req;
	
	//增
	@RequestMapping(value="/addSong", method = RequestMethod.POST)
	@ResponseBody
	public MsgObj addSong(@RequestParam Map<String,Object> params) {
		System.out.println(params);
		return msg.getMsgObj(service.addSong(params)==1, "添加歌曲成功", "添加歌曲失败");		
	}
	
	@RequestMapping("/upload")    
    @ResponseBody    
    public MsgObj handleFileUpload(@RequestParam("file") MultipartFile file) {  
		MsgObj msgObj = null;
        if (!file.isEmpty()) {    
            try {    
                BufferedOutputStream out = new BufferedOutputStream(    
                        new FileOutputStream(new File(file.getOriginalFilename())));
                String fileName = file.getName();
				System.out.println(fileName);
				String uuid = UUID.randomUUID().toString().replaceAll("-", "");
				System.out.println(uuid);
				String extension = FilenameUtils.getExtension(fileName);
				String newFileName = uuid+"."+extension;
				
				String contentType = file.getContentType();
				InputStream inputStream = file.getInputStream();
				String realPath = req.getServletContext().getRealPath("/upload")+"/";
				System.out.println("realPath:"+realPath);
				File targetFile = new File(realPath);
				if(!targetFile.exists()){
					targetFile.mkdir();
				}				
				OutputStream outputStream = new FileOutputStream(new File(targetFile,newFileName));
				IOUtils.copy(inputStream, outputStream);
				msgObj = new MsgObj(1,"/upload/"+newFileName);                
                out.write(file.getBytes());  
                out.flush();    
                out.close();    
            } catch (FileNotFoundException e) {    
                e.printStackTrace();    
                return new MsgObj(0,"服务器异常,稍后再试!!!");    
            } catch (IOException e) {    
                e.printStackTrace();    
                return new MsgObj(0,"服务器异常,稍后再试!!!"); 
            }    
            return msgObj;    
        } else {    
            return new MsgObj(0,"上传失败，因为文件是空的");
        } 
    }
	
	//删
	@RequestMapping(value="/deleteSong")
	@ResponseBody
	public MsgObj deleteSong(String song_ids) {
		System.out.println(song_ids);
		return msg.getMsgObj(service.deleteSong(song_ids)>=1, "删除歌曲成功", "删除歌曲失败");			
	}
	
	//改
	@RequestMapping(value="/updateSong")
	@ResponseBody
	public MsgObj updateSong(@RequestParam Map<String,Object> params) {
		return msg.getMsgObj(service.updateSong(params)==1, "修改歌曲信息成功", "修改歌曲信息失败");		
	}
	
	//查
	@RequestMapping(value="/songList")
	@ResponseBody
	public Map<String,Object> selectSong(SongQueryObject qo) {
		System.out.println(qo);
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("beginIndex", qo.getBeginIndex());
		params.put("rows", qo.getRows());
		if (StringUtils.isNotBlank(qo.getCd_name_key())) {
			params.put("cd_name", qo.getCd_name_key());
		}
		if (StringUtils.isNotBlank(qo.getSong_name_key())) {
			params.put("song_name", qo.getSong_name_key());
		}
		if (StringUtils.isNotBlank(qo.getSinger_name_key())) {
			params.put("singer_name", qo.getSinger_name_key());
		}
		if (StringUtils.isNotBlank(qo.getPublish_begin_key())) {
			params.put("publish_begin", qo.getPublish_begin_key());
		}
		if (StringUtils.isNotBlank(qo.getPublish_end_key())) {
			params.put("publish_end", qo.getPublish_end_key());
		}
		if (StringUtils.isNotBlank(qo.getLang_id_key())) {
			params.put("lang_id", qo.getLang_id_key());
		}
		System.out.println(params);
		return service.selectDataByCondition(params);
	}
	
	//page
	@RequestMapping("/songPage")
	public String adminPage() {
		return "songPage";
	}
	
}
